package com.undsf.crawler

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

/**
 * Created by Arathi on 2017/8/16.
 */
abstract class BaseCrawler implements ICrawler {
    OkHttpClient httpClient

    BaseCrawler() {
        httpClient = new OkHttpClient()
    }

    def abstract crawl()

    def sendGetRequest(String url) {
        Request request = new Request.Builder()
                .url(url)
                .build()
        Response response = httpClient.newCall(request).execute()
        return response.body().string()
    }

    def downloadFile(String url, String local) {
        println "正在下载 " + url + " 到 " + local

        Request request = new Request.Builder()
                .url(url)
                .build()
        Response response = httpClient.newCall(request).execute()

        try {
            InputStream inStream = response.body().byteStream()
            File file=new File(local)
            OutputStream os = new FileOutputStream(file)
            byte[] buffer = new byte[4*1024]
            int len
            while((len = inStream.read(buffer)) != -1){
                os.write(buffer,0,len)
            }
            os.flush()
        }
        catch (IOException ex) {
            ex.printStackTrace()
        }
        finally {
        }
    }
}
